package com.hangzhou.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hangzhou.mapper.RoomBookMapper;
import com.hangzhou.mapper.UserMapper;
import com.hangzhou.pojo.*;
import com.hangzhou.service.RoomBookService;
import com.hangzhou.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.awt.print.Book;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@Service //@Service： 将Service的实现对象交给IOD容器
public class RoomBookServiceImpl implements RoomBookService {
  @Autowired
  private RoomBookMapper roomBookMapper;
  @Autowired
  private UserMapper userMapper;

  @Override
  public void add(RoomBook roomBook) {
    roomBookMapper.add(roomBook);
  }

  @Override
  public PageBean<RoomBookDetailDTO> listApprove(
    Integer pageNum, 
    Integer pageSize, 
    String roomname, 
    String booker, 
    String bookphone,
    Integer bookerid
  ) {
    //1.创建PageBean对象
    PageBean<RoomBookDetailDTO> pb = new PageBean<>();

    //2.开启分页查询 PageHelper
    PageHelper.startPage(pageNum,pageSize);

    //3.调用mapper
    Map<String,Object> map = ThreadLocalUtil.get();
    List<RoomBookDetailDTO> as = roomBookMapper.listApprove(roomname, booker, bookphone, bookerid);
    Page<RoomBookDetailDTO> p = (Page<RoomBookDetailDTO>) as;

    pb.setTotal(p.getTotal());
    pb.setItems(p.getResult());
    return pb;
  }

  @Override
  public PageBean<RoomBookDetailDTO> listRecord(
          Integer pageNum,
          Integer pageSize,
          String roomname,
          String booker,
          String bookphone,
          Integer bookerid
  ) {
    // 1. 创建PageBean对象
    PageBean<RoomBookDetailDTO> pb = new PageBean<>();

    // 2. 获取当前用户信息
    Map<String,Object> map = ThreadLocalUtil.get();
    String username = map.get("username").toString();
    User user = userMapper.findByUserName(username);

    // 3. 开启分页查询（必须在查询前调用）
    PageHelper.startPage(pageNum, pageSize);

    // 4. 执行查询
    List<RoomBookDetailDTO> as;
    if (user.getUserRole().equals("ADMIN")) {
      as = roomBookMapper.listRecord(roomname, booker, bookphone, bookerid);
    } else if (user.getUserRole().equals("USER")) {
      as = roomBookMapper.listRecordByUser(roomname, booker, bookphone, bookerid, user.getId());
    } else {
      // 处理未知角色
      pb.setTotal(0L);
      pb.setItems(Collections.emptyList());
      return pb;
    }

    // 5. 安全转换（确保是Page类型）
    if (as instanceof Page) {
      Page<RoomBookDetailDTO> p = (Page<RoomBookDetailDTO>) as;
      pb.setTotal(p.getTotal());
      pb.setItems(p.getResult());
    } else {
      // 如果未被PageHelper包装，手动处理分页
      pb.setTotal((long) as.size());
      pb.setItems(as);
    }

    return pb;
  }

  @Override
  public void deleteById(Integer id) {
    roomBookMapper.deleteById(id);
  }

  // 取消预约
  @Override
  public void cancel(RoomBook roomBook) {
    roomBookMapper.cancel(roomBook);
  };

  // 审核通过&&不通过
  @Override
  public void approve(RoomBook roomBook) {
//    roomBookMapper.approve(roomBook);
    int cont = roomBookMapper.listRecordByRoomId(roomBook.getRoomid(),roomBook.getStartTime(),roomBook.getEndTime());
    if (cont == 0) {
      roomBookMapper.approve(roomBook);
    }else {
      throw new RuntimeException("该时间段已被预约");
    }

  };
  @Override
  public void noapprove(RoomBook roomBook) {
    roomBookMapper.noapprove(roomBook);
  }
}
